### Replication data for: Ethnic Minorities, Interstate War, and Popular Support for Fiscal Capacity Development
### Data for the cantons of Fribourg & Valais as well as most municipalities of Switzerland
### Author: Andre Walter & Patrick Emmenegger
### Contact: andre.walter[at]ipz.uzh.ch

### Install rdrobust package version 2.0.1 to replicate the results (package "devtools" required)

devtools::install_version("rdrobust", version = "2.0.1", repos = "http://cran.us.r-project.org")

### Packages

library(rdrobust)
library(cowplot)
library(sandwich)
library(lmtest)
library(texreg)
library(tidyverse)
library(sf)

rm(list=ls())

### Data

load("swissdat.RData")


##### Figure A1: Maps

mapdat <- geo %>% mutate(GMDE = as.numeric(GMDE)) %>% filter(GMDE<7000)
 
ggplot() +
  geom_sf(data = mapdat, aes(fill = frash)) + theme_map()+labs(fill = "Share French Speaker") + 
  scale_fill_distiller(palette = "Oranges", type="seq", direction = 1)+ theme(legend.position = c(.85, .8))


ggplot() +
  geom_sf(data = mapdat, aes(fill = yessh)) + theme_map()+labs(fill = "Yes Share Direct Taxation") + 
  scale_fill_distiller(palette = "Oranges", type="seq", direction = 1)+ theme(legend.position = c(.85, .8))


##### Figure A5: Fribourg & Valais

### Compute distance from language border

rom <- geo %>% filter(Kanton %in% c(10,23)) 

test <- st_intersection(rom %>% filter(fradum==1) %>% st_cast("MULTILINESTRING"),
                        rom %>% filter(fradum==0) %>% st_cast("MULTILINESTRING"))


rom$dist <- apply(st_distance(st_centroid(rom),test)/1000,1, function(x){min(x)})

rom <- rom %>% mutate(dist = ifelse(fradum == 0, dist * -1, dist))

vs <- rom %>% filter(Kanton %in% c(23))

fr <- rom %>% filter(Kanton %in% c(10))

n <- 25
m <- 6

refr <- matrix(NA, ncol = m, nrow = n)
revs <- matrix(NA, ncol = m, nrow = n)

for (i in c(2:n)){
  
  rddfr <- rdrobust(fr$yessh, fr$dist, h = i)
  
  refr[i,] <- c(0,i, rddfr$Estimate[[1]], rddfr$ci[[1]], rddfr$ci[[4]],sum(rddfr$N_h))
  
  rddvs <- rdrobust(vs$yessh, vs$dist, h = i)
  
  revs[i,] <- c(1,i, rddvs$Estimate[[1]], rddvs$ci[[1]], rddvs$ci[[4]],sum(rddvs$N_h))
  
}

rddres <- bind_rows(data.frame(refr),data.frame(revs))

rddres <- rddres %>% drop_na() %>% rename(canton = X1, bw = X2, est = X3, low = X4, upp = X5, obs = X6) %>%
  mutate(bwop = ifelse(bw == round(rdrobust(fr$yessh, fr$dist)$bws[[1]],0) & canton == 0, 1,
                       ifelse(bw == round(rdrobust(vs$yessh, vs$dist)$bws[[1]],0) & canton == 1, 1,0)),
         bwop = factor(bwop), canton = ifelse(canton == 1, "Valais", "Fribourg"))

estfig <- ggplot(data = rddres, aes(y = est, x = bw, group = canton, linetype = canton, color = as.factor(bwop)))+
  geom_point(aes(),position=position_dodge(0.5))+theme_bw()+
  geom_errorbar(aes(ymin = low, ymax = upp),position=position_dodge(0.5))+scale_color_manual(values = c("black","red"),guide = FALSE)+
  theme(axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black"),
        panel.border = element_blank())+labs(title = "", x = "", group = "Canton",
                                             y = "Treatment Effect")+ geom_hline(yintercept = 0, size = 0.2)+
  scale_linetype_manual(name = "Canton", values = c(1,2))

estobs <- ggplot(data = rddres, aes(y = obs, x = bw, group = canton, linetype = canton))+geom_line(aes())+theme_bw()+
  theme(axis.line.x = element_line(colour = "black"), axis.line.y = element_line(colour = "black"),
        panel.border = element_blank())+labs(title = "", x = "Bandwidth",
                                             y = "Number of Observations")+ geom_hline(yintercept = 0, size = 0.2)+
  scale_linetype_manual(name = "Canton", values = c(1,2))

plot_grid(estfig,estobs, nrow = 2, align = "v", rel_heights = c(3, 1))

##### Table A3: OLS Switzerland

m1 <- lm(yessh2~nonger, geo)
m1se <- coeftest(m1,vcovCL(m1, ~Kanton))[,2]
m1p <- coeftest(m1,vcovCL(m1, ~Kanton))[,4]

m2 <- lm(yessh2~nonger+firsh+popdens+catsh+elev+rough_mean+Potatoes_m+Wheat_mean+Grund_mean+Skelet_mea+Naehr_mean+Vernaess_m+Wassersp_m+Wasser_mea, geo)
m2se <- coeftest(m2,vcovCL(m2, ~Kanton))[,2]
m2p <- coeftest(m2,vcovCL(m2, ~Kanton))[,4]

m3 <- lm(yessh~nonger, geo)
m3se <- coeftest(m3,vcovCL(m3, ~Kanton))[,2]
m3p <- coeftest(m3,vcovCL(m3, ~Kanton))[,4]

m4 <- lm(yessh~nonger+firsh+popdens+catsh+elev+rough_mean+Potatoes_m+Wheat_mean+Grund_mean+Skelet_mea+Naehr_mean+Vernaess_m+Wassersp_m+Wasser_mea, geo)
m4se <- coeftest(m4,vcovCL(m4, ~Kanton))[,2]
m4p <- coeftest(m4,vcovCL(m4, ~Kanton))[,4]


texreg(list(m1,m2,m3,m4), symbol = "\\dagger", override.se = list(m1se,m2se,m3se,m4se),
       override.pvalues = list(m1p,m2p,m3p,m4p),
       booktabs = T, dcolumn = T, use.packages = F, 
       caption = "Ethnic-Linguistic Minorities \\& Support for Direct Taxation: Country Sample", label = "OLS",
       custom.coef.map =  list("nonger" = "Share Non-German Speaker", "firsh" = "Share Agricultural Employment","popdens" = "Population Density",
                               "catsh" = "Share Catholics","elev" = "Elevation","rough_mean" = "Terrain Roughness",
                               "Potatoes_m" = "Climate Potatoes", "Wheat_mean" = "Climate Wheat","Grund_mean" = "Soil Thoroughness",
                               "Skelet_mea" = "Stone Content","Naehr_mean" = "Nutrient Retention Capacity","Vernaess_m" = "Water Logging",
                               "Wassersp_m" = "Water Retention Capacity","Wasser_mea" = "Water Permeability",
                               "(Intercept)" = "Intercept"), #sideways = T, 
       custom.header = list("Temporary Taxation 1915" = 1:2,"Permanent Taxation 1918" = 3:4),
       custom.note = "Cluster-robust standard-errors (Cantons) in parenthesis. %stars.",stars = c(0.001, 0.01, 0.05, 0.1))